package cn.htsyb.synergy.security.web.model;

import cn.htsyb.synergy.orm.constant.ValidStatus;
import cn.htsyb.synergy.orm.jpa.model.entity.AbsIdStatusEntity;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import jakarta.persistence.Transient;
import java.util.Collection;
import java.util.Date;

/**
 *
 * 此类的主要作用扩展携带ID ,方便程序内部直接获取授权用户的主键（另一种做法是使用JWT扩展，将主键ID写入token）
 * @author lakeside hubin_yuan.410@hotmail.com
 * @version V1.0.0
 * @date 2022/5/22
 * @since 1.0
 */
@Setter
@Getter
@NoArgsConstructor
public class SecurityUserDetail extends AbsIdStatusEntity implements UserDetails {
    @Transient
    @JsonProperty(access = JsonProperty.Access.WRITE_ONLY)
    protected Collection<? extends GrantedAuthority> authorities;
    private  String username;
    @JsonIgnore
    private  String password;
    private Date expiredAt;

    public SecurityUserDetail(String id, String username, String password, Date expiredAt, ValidStatus validStatus) {
        this.id=id;
        this.username=username;
        this.password=password;
        this.expiredAt=expiredAt;
        this.validStatus=validStatus;
    }


    @Override
    @JsonIgnore
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return this.authorities;
    }

    /* private static final List<GrantedAuthority> ROLE_USER = Collections
            .unmodifiableList(AuthorityUtils.createAuthorityList("ROLE_USER"));
    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return ROLE_USER;
    }*/

    @Override
    public String getUsername() {
        return username;
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return true;
    }
}
